$(function(){

  class Register{
    constructor(){
      this.user_name = $('.register .register_form .user-name input');
      this.user_psd = $('.register .register_form .user-psd input')
      this.rpt_psd = $('.register .register_form .rpt-psd input')
      this.nickname = $('.register .register_form .nickname input')
      this.btn = $('.register .register_form .register-btn')
      this.tips = $('.register .msg .tips')
      this.msg = $('.register .msg')
      this.bindEvent()
    }

    bindEvent(){
      this.btn.on('click',()=>{
        this.msg.css({
          display : 'block'
        })
        this.request();
      })
    }
    request(){
      let options = {
        url:'http://localhost:8888/users/register',
        type:'POST',
        headers:'Content-Type:application/x-www-form-urlencoded',
        data:{
          username:this.user_name.val(),
          password:this.user_psd.val(),
          rpassword:this.rpt_psd.val(),
          nickname:this.nickname.val()
        }    
      }

      $.ajax(options).then(res=>{
        console.log(res);
        switch(res.code){
          case 1:
            this.success();
            break;
          default:
            this.err(res)
            break;
        }
      })
    }

    err(res){
      this.tips.text('注册失败,请重新注册')
      setTimeout(()=>{
        location.reload()
      },1000)
    }
    success(){
      this.tips.text('注册成功，稍后为您跳转到登录页面！')
      sessionStorage.setItem('usermsg',JSON.stringify({
        username:this.user_name.val(),
        password:this.user_psd.val(),
      }))
      setTimeout(()=>{
        location.href = '../../dist/login.html'
      },10000)
    }
  }
  new Register()
})